package org.topera.padelrank.database;

import java.util.ArrayList;

import org.topera.padelrank.database.modelo.ModeloDB;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBHelper extends SQLiteOpenHelper{

	private static final String DATABASE_NAME 	 = "PadelRank.sqlite";
	private static final int 	DATABASE_VERSION = 0;	

	public DBHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		
		ModeloDB tablas = new ModeloDB();
		tablas.CargaModeloDatos(db);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		
		Log.w(DBHelper.class.getName(), "Actualizando la base de datos desde la version " + oldVersion + " a "
				+ newVersion + ". Todos los datos antiguos seran destruidos");
		
		ModeloDB tablas = new ModeloDB();
		tablas.BorraModeloDatos(db);
		
		onCreate(db);
  }
	
	public void commitPoolUpdate(ArrayList<String> listSQL){
		
		if(listSQL != null && listSQL.size() > 0){
			SQLiteDatabase db = getWritableDatabase();
			if(db!=null){
				db.beginTransaction();
				for(int i = 0; i < listSQL.size();i++){
					db.execSQL(listSQL.get(i));
				}
				db.setTransactionSuccessful();
				db.endTransaction();
				db.close();
			}
			listSQL.clear();
		}		
	}	
} 